package com.example.websocket.config;

import com.example.websocket.handler.TestWebSocketHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
    @Autowired
    private TestWebSocketHandler testWebSocketHandler; // 注入实现的TestWebSocketHandler
    /**
     * 将 testWebSocketHandler 与 /test 路径进行绑定
     * 当客户端访问 /test 路径时，就会触发执行相关方法
     * @param registry 用于注册 WebSocket 端点和配置的对象
     */
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(testWebSocketHandler, "/test");
    }
}
